<?php


namespace app\common\model;


use think\Db;
use think\Model;

class CompanyModel extends Model
{
    protected $name = 'company';


    /**
     * 获取公司分页列表
     * @param $search
     * @param $region
     * @param $money
     * @param $region_id
     * @param $industry_id
     * @param $position_id
     * @param $pay_money_id
     * @param $work_type_id
     * @param $page
     * @param $limit
     * @return array
     */
    public function getPageList($search, $region, $money, $region_id, $industry_id, $position_id, $pay_money_id, $work_type_id, $page, $limit): array
    {
        try {
            $where = [
                'status' => '1',
            ];
            $company_position_where = [];
            $find_in_set_where = [];
            $where_or = [];
            if($search) {
                $company_position_where['name'] = ['like',"%{$search}%"];
            }
            if($region) {
                $rid = model('app\common\model\RegionModel')->where(['name' => ['like',"%$region%"]])->value('id');
                $find_in_set_where = "FIND_IN_SET('$rid',`region_ids`)";
            }
            if($money) {
                $company_position_where['min_money'] = ['>=',$money];
                $company_position_where['max_money'] = ['<=',$money];
            }


//            if($region_id) {
//                $find_in_set_where = "FIND_IN_SET('$region_id',`region_ids`)";
//            }
//            if($industry_id) {
//                $company_position_where['industry_id'] = $industry_id;
//            }
//            if($position_id) {
//                $company_position_where['position_id'] = $position_id;
//            }
//            if($pay_money_id) {
//                $pay_money = model('app\common\model\PayMoneyModel')->get($pay_money_id);
//                if($pay_money) {
//                    $company_position_where['min_money'] = ['>',$pay_money->money_min];
//                    $company_position_where['max_money'] = ['<',$pay_money->money_max];
//                }
//                $company_position_where['position_id'] = $position_id;
//            }
            if($work_type_id) {
                $company_position_where['work_type_id'] = $work_type_id;
            }
            if(!empty($company_position_where) || !empty($find_in_set_where)) {
                $company_position_where['status'] = '1';
                $company_ids = model('app\common\model\CompanyPositionModel')
                    ->where($company_position_where)
                    ->where($find_in_set_where)
                    ->whereOr($where_or)
                    ->column('company_id');
                $where['id'] = ['in',$company_ids];
            }
            $field = ['id','name','tag','image','file','num','limit_condition'];
            $order = [
                'weigh' => 'asc'
            ];
            $list = $this
                ->where($where)
                ->field($field)
                ->order($order)
                ->paginate($limit,false,['page' => $page]);
            foreach ($list as $row) {
                $row->position = model('app\common\model\CompanyPositionModel')->where(['company_id' => $row->id])->order('weigh','asc')->find();
                if($row->position) {
                    $row->position->image = ImageAddDomain($row->position->image);
                    $row->position->file = ImageAddDomain($row->position->file);
                }
//                $row->tag = explode(',',$row->tag);
                $row->image = ImageAddDomain($row->image);
                $row->file = ImageAddDomain($row->file);
                $position_tag = model('app\common\model\CompanyPositionModel')->where(['company_id' => $row->id])->column('tag');
                $row->tag = [];
                if($position_tag) {
                    $position_tag = implode(',',$position_tag);
                    $position_tag = explode(',',$position_tag);
                    $row->tag = array_unique($position_tag);
                }
                $position_title = model('app\common\model\CompanyPositionModel')->where(['company_id' => $row->id])->column('name');
                $row->position_title = implode('/',$position_title);
                $row->min_money = model('app\common\model\CompanyPositionModel')->where(['company_id' => $row->id])->value('min_money');
                $row->max_money = model('app\common\model\CompanyPositionModel')->where(['company_id' => $row->id])->value('max_money');
                $row->num = model('app\common\model\CompanyPositionModel')->where(['company_id' => $row->id])->sum('num');
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 获取公司职位详情
     * @param $company_id
     * @return array
     */
    public function getDetail($company_id): array
    {
        try {
            $data = $this->get($company_id,['positions' => function($q){
                $q->where('status','1');
                $q->order('weigh','asc');
            }]);
            if($data) {
                $data->tag = explode(',',$data->tag);
                $data->image = ImageAddDomain($data->image);
                $data->file = ImageAddDomain($data->file);
                if($data->positions) {
                    foreach ($data->positions as $row) {
                        $row->tag = explode(',',$row->tag);
                        $row->image = ImageAddDomain($row->image);
                        $row->file = ImageAddDomain($row->file);
                        $region_text_arr = model('app\common\model\RegionModel')->whereIn('id',$row->region_ids)->column('name');
                        $row->region_text = implode(',',$region_text_arr);
                    }
                }
            }

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $data
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    public function position(){
        return $this->hasOne('app\common\model\CompanyPositionModel','company_id','id');
    }

    public function positions(){
        return $this->hasMany('app\common\model\CompanyPositionModel','company_id','id');
    }


}